package com.zzj.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzj.entity.File;
import com.zzj.entity.Folder;
import com.zzj.entity.User;
import com.zzj.mapper.FolderMapper;
import com.zzj.service.IFolderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 文件夹表 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-11-14
 */
@Service
public class FolderServiceImpl extends ServiceImpl<FolderMapper, Folder> implements IFolderService {

    @Override
    public Folder create(String creatorId, String pathName, String parentId) {
        String[] split = pathName.split("/");
        String name = split[split.length - 1];
        Folder folder = new Folder();
        folder.setName(name);
        folder.setCreatorId(creatorId);
        folder.setParentId(parentId);
        if (this.save(folder)) {
            return folder;
        }
        return null;
    }


    @Override
    public boolean checkExist(String creatorId, String pathName, String parentId) {
        String[] split = pathName.split("/");
        String name = split[split.length - 1];
        Folder one = this.getOne(Wrappers.<Folder>lambdaQuery()
                .eq(Folder::getParentId, parentId)
                .eq(Folder::getName, name)
                .eq(Folder::getCreatorId, creatorId), false);
        return one != null;
    }

    //只检测最近父层
    //应该递归检测到顶层
    @Override
    public boolean checkParentValid(String creatorId, String pathName, String parentId) {
        String[] split = pathName.split("/");
        if (split.length == 1) {
            return false;
        }
        //顶层有效
        if (Objects.equals(parentId, "") && split.length == 2) {
            return true;
        } else {
            //核对父文件夹
            String parentName = split[split.length - 2];
            Folder one = this.getOne(Wrappers.<Folder>lambdaQuery()
                    .eq(Folder::getId, parentId)
                    .eq(Folder::getName, parentName)
                    .eq(Folder::getCreatorId, creatorId), false);
            return one != null;
        }
    }

    @Override
    public List<Folder> queryList(String creatorId, String parentId) {
        return this.list(Wrappers.<Folder>lambdaQuery()
                .eq(Folder::getCreatorId, creatorId)
                .eq(Folder::getParentId, parentId)
                .orderByDesc(Folder::getCreatedAt));
    }

    @Override
    public List<Folder> queryList(String creatorId, String parentId, String searchKey) {

        return this.list(Wrappers.<Folder>lambdaQuery()
                .eq(Folder::getCreatorId, creatorId)
                .eq(Folder::getParentId, parentId)
                .like(Folder::getName, searchKey)
                .orderByDesc(Folder::getCreatedAt));
    }

    @Override
    public List<Folder> queryList(String creatorId, String searchKey, boolean f) {
        return this.list(Wrappers.<Folder>lambdaQuery()
                .eq(Folder::getCreatorId, creatorId)
                .like(Folder::getName, searchKey)
                .orderByDesc(Folder::getCreatedAt));
    }

    @Override
    public boolean updateName(String id, String parentId, String name) {
        Folder one = this.getOne(Wrappers.<Folder>lambdaQuery()
                .eq(Folder::getParentId, parentId)
                .eq(Folder::getName, name), false);
        if(Objects.equals(parentId, "")){
            one = null;
        }
        if (one == null) {
            Folder folder = new Folder();
            folder.setId(Integer.valueOf(id));
            folder.setName(name);
            return this.updateById(folder);
        }
        return false;
    }

    @Override
    public List<Folder> getFolderList(String parentId) {
        return this.list(Wrappers.<Folder>lambdaQuery()
                .eq(Folder::getParentId, parentId)
                .orderByDesc(Folder::getCreatedAt));
    }

    @Override
    public Folder getById(String id) {
        return this.getOne(Wrappers.<Folder>lambdaQuery()
                .eq(Folder::getId, id), false);
    }


}
